package com.lahuan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lahuan.demo.entity.Order;
import com.lahuan.demo.service.OrderService;
import com.lahuan.demo.service.impl.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author linfy
 */
@Slf4j
@EnableSwagger2
@SpringBootApplication
public class BaseSpringBootApplication implements ApplicationRunner {
    @Autowired
    OrderService orderService;

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(BaseSpringBootApplication.class, args);
        DataSource dataSource = ctx.getBean(DataSource.class);
        log.info("当前使用的数据源:" + dataSource.getClass());
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //生产者
        new Thread(() -> {
            while (true){
                List<Order> orderList = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    Order ord = new Order();
                    ord.setOrderSn(UUID.randomUUID().toString().substring(0,16));
                    ord.setOrderStatus("0");
                    ord.setUserId(1L);
                    ord.setIsPay(1);
                    orderList.add(ord);
                }
                System.out.println("生产了"+orderList.size()+"个订单");
                orderService.saveBatch(orderList);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //消费
        new Thread(() -> {
           while (true){
               List<Order> orderList = orderService.list(
                       new LambdaQueryWrapper<Order>().eq(Order::getOrderStatus, "0").last(" limit 100 "));
               if(orderList.size()==0){
                   try {
                       Thread.sleep(100);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   continue;
               }
               for (Order o : orderList) {
                   o.setOrderStatus("1");

                   //通过乐观锁机制保证多个消费程序协作
                   //同时对处理失败的订单不做任何处理，等下一批捞数据时一起处理
                   orderService.update(new LambdaUpdateWrapper<Order>()
                           .set(Order::getOrderStatus,"1")
                           .eq(Order::getId,o.getId())
                           .eq(Order::getOrderStatus,"0")
                   );

               }
               System.out.println("消费了"+orderList.size()+"个订单");
           }
        }).start();
    }
}
